package cn.tedu.lucky.draw.server.service.impl;

import cn.tedu.lucky.draw.server.common.enums.DrawEnum;
import cn.tedu.lucky.draw.server.common.enums.ErrorEnum;
import cn.tedu.lucky.draw.server.common.handler.DrawException;
import cn.tedu.lucky.draw.server.protocol.po.ActivityPrize;
import cn.tedu.lucky.draw.server.protocol.po.Prize;
import cn.tedu.lucky.draw.server.protocol.query.PrizeQuery;
import cn.tedu.lucky.draw.server.repository.ActivityPrizeRepository;
import cn.tedu.lucky.draw.server.repository.PrizeRepository;
import cn.tedu.lucky.draw.server.service.IPrizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class PrizeServiceImpl implements IPrizeService {

    @Autowired
    private PrizeRepository prizeRepository;

    @Autowired
    private ActivityPrizeRepository activityPrizeRepository;

    List<Prize> prizeList = null;

    @Override
    public List<Prize> findAll(PrizeQuery prizeQuery) {

        if (prizeList == null) {
            prizeList = prizeRepository.findAll();
        }
        //奖品名称
        String name = prizeQuery.getName();
        //奖品状态
        Integer status = prizeQuery.getStatus();

        /**
         * name：拯救者
         * 第一个：拯救者
         * 第二个：HUAWEI META 10
         */
        return prizeList.stream()
                .filter(prize -> (name == null || name == "" || name.equals(prize.getName())))
                .filter(prize -> (status == null || status.equals(prize.getStatus())))
                .collect(Collectors.toList());
    }

    @Override
    public void save(Prize prize) {
        LocalDateTime now = LocalDateTime.now();
        prize.setCreateTime(now);
        prize.setUpdateTime(now);
        Prize savePrize = prizeRepository.save(prize);
        if (savePrize == null) {
            throw new DrawException(ErrorEnum.SYSTEM_ERROR);
        }
        prizeList = null;

    }

    @Override
    public List<Prize> findAllByActivityId(Long activityId) {
        //获取到活动奖品的关联对象
        List<ActivityPrize> activityPrizes = activityPrizeRepository.findByActivityId(activityId);
        List<Long> prizeIds = activityPrizes.stream()
                .map(ActivityPrize::getPrizeId)
                .collect(Collectors.toList());
        //根号关联对象的奖品ID获取到奖品信息
        List<Prize> prizeList = prizeRepository.findAllById(prizeIds);
        return prizeList;
    }

    /**
     * 根据活动ID获取未绑定奖品，也就是可绑定的奖品
     */
    @Override
    public List<Prize> findNoBindPrizeByActivityId(Long activityId) {
        //过滤已绑定奖品
        List<ActivityPrize> activityPrizes = activityPrizeRepository.findByActivityId(activityId);
        List<Long> excludedIds = activityPrizes.stream().map(ActivityPrize::getPrizeId).collect(Collectors.toList());
        //获取可绑定的奖品
        //奖品状态
        Integer stauts = Integer.valueOf(DrawEnum.ENABLE.getCode());

        if (excludedIds==null || excludedIds.isEmpty()){
            return prizeRepository.findByStatusEquals(stauts);
        }

        return prizeRepository.findByIdNotInAndStatusEquals(excludedIds, stauts);
    }
}
